package com.pingsky.apm.agent.core.service.config;

import com.pingsky.apm.agent.api.AgentDir;
import com.pingsky.apm.agent.api.config.AgentConfig;
import com.pingsky.apm.agent.core.util.FileUtil;
import com.pingsky.apm.agent.core.util.NumberUtil;
import com.pingsky.apm.agent.core.util.StringUtil;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

public class DefaultAgentConfig implements AgentConfig {

    private Properties properties;

    @Override
    public String readString(String propertyName, String defaultValue) {
        return properties.getProperty(propertyName, defaultValue);
    }

    @Override
    public int readInt(String propertyName, int defaultValue) {
        String value = properties.getProperty(propertyName);
        return NumberUtil.parseInteger(value, defaultValue);
    }

    @Override
    public long readLong(String propertyName, long defaultValue) {
        String value = properties.getProperty(propertyName);
        return NumberUtil.parseLong(value, defaultValue);
    }

    @Override
    public boolean readBoolean(String propertyName, boolean defaultValue) {
        String value = properties.getProperty(propertyName, Boolean.toString(defaultValue));
        return Boolean.parseBoolean(value);
    }

    @Override
    public List<String> readList(String propertyName) {
        String value = properties.getProperty(propertyName);
        if (StringUtil.isEmpty(value)) {
            return Collections.emptyList();
        }
        return Arrays.asList(value.split(","));
    }

    @Override
    public void prepare() throws Throwable {
        this.properties = FileUtil.loadProperties(AgentDir.getInstance().getConfigFile());
    }

    @Override
    public void start() throws Throwable {

    }
}
